﻿using System;
using System.Collections.Generic;
using System.Text;

namespace StarBotDotNet.TerranExample1
{
    public class MiningParty : UnitParty
    {
        private int priority = int.Parse(System.Configuration.ConfigurationSettings.AppSettings["MiningBotsPriority"]);
        
        Dictionary<StarBotDotNet.UnitType, int> requiredUnits;

        public MiningParty(ProxyBot bot, int priority)
            : this(bot)
        {
            this.priority = priority;
        }

        public MiningParty(ProxyBot bot)
        {
            requiredUnits = new Dictionary<UnitType, int>();
            requiredUnits.Add(bot.GetCurrentPlayerWorkerType(), int.Parse(System.Configuration.ConfigurationSettings.AppSettings["MiningBotsCount"]));
        }

        public override void ExecuteTactics(ProxyBot bot, UnitDictionary currentUnits, int currentTime)
        {
            foreach (UnitData unit in this.GetAllUnits())
            {
                // make idle workers mine
                if (unit.OwnedByPlayer && unit.IsIdle && unit.IsWorker)
                {
                    int closestID = -1;
                    double closest = System.Double.MaxValue;

                    foreach (Unit patch in bot.Units)
                    {

                        if (patch.Type.Id == (int)Constants.UnitTypes.Resource_Mineral_Field)
                        {
                            double distance = (0.5 + SupportClass.Random.NextDouble()) * unit.Unit.distance(patch);
                            if (distance < closest)
                            {
                                closest = distance;
                                closestID = patch.ID;
                            }
                        }
                    }

                    if (closestID != -1)
                    {
                        Utilities.WriteColorLine(ConsoleColor.Cyan, "Right on patch: " + unit.Unit.ID + " " + closestID);
                        bot.rightClick(unit.Unit.ID, closestID);
                    }
                }
            }
        }

        public override int Priority
        {
            get { return priority; }
        }

        public override Dictionary<UnitType, int> RequiredUnits
        {
            get { return requiredUnits; }
        }

        protected override bool IsUnused(UnitData data)
        {
            return data.IsIdle;
        }
    }
}
